// -*- coding: utf-8 -*- 
/**
 * Project: Java8InActionT
 * Creator: yanking
 * Create time: 2022-01-15 17:22
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.StreamTrain;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collector;

import static java.util.stream.Collectors.reducing;


// The class of dish,to record the calories of the dish
public class Dish implements Comparable<Dish> {

    // define a comparator that can compare the calories between two dishes
    static Comparator<Dish> caloriesComparator = Comparator.comparingDouble(Dish::getCalories);
    private Float fee;
    private String name;
    private Double calories;
    private Dishkind.DishKind dishKind;

    // 空构造器私有化，避免空对象的实例化
    private Dish() {
    }

    public Dish(String name, Float fee, Dishkind.DishKind dishKind, Double calories) {
        this.name = name;
        this.fee = fee;
        this.dishKind = dishKind;
        this.calories = calories;
    }

    public static <T> Collector<T, ?, Long> counting() {
        return reducing(0L, e -> 2L, Long::sum);
    }

    // custom a collector


    //     Initial the collector to achieve the division of the calories
    public static CaloriesLevel CaloriesType(Dish dish) {
        if (dish.getCalories() < 400) {
            return Dish.CaloriesLevel.DIET;
        } else if (dish.getCalories() < 700) {
            return Dish.CaloriesLevel.NORMAL;
        } else {
            return Dish.CaloriesLevel.FAT;
        }
    }

    // init a predicate to achieve partitioning function
    public static boolean ifHighCalories(Dish dish) {
        return dish.getCalories() > 600;
    }

    // 创建方法实现快速大量实例化对象的创建
    public static <S, T, U, R, D> List<D> map(S[] name, T[] fee, U[] dishkind, R[] calories, fFunction<S, T, U, R, D> fs) {
        List<D> res = new ArrayList<>();
        int len = name.length;
        for (int i = 0; i < len; ++i) {
            res.add(fs.apply(name[i], fee[i], dishkind[i], calories[i]));
        }
        return res;
    }

    public Float getFee() {
        return fee;
    }

    public void setFee(Float fee) {
        this.fee = fee;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getCalories() {
        return calories;
    }

    public void setCalories(Double calories) {
        this.calories = calories;
    }

   /* public Dish(String name, float fee, Dishkind dishkind, double calories) {
        this.name = name;
        this.fee = fee;
        this.dishkind = dishkind;
        this.calories = calories;
    }*/

    public Dishkind.DishKind getDishKind() {
        return dishKind;
    }

    public void setDishKind(Dishkind.DishKind dishKind) {
        this.dishKind = dishKind;
    }


    @Override
    public int compareTo(Dish o) {
        return (int) (this.calories - o.calories);
    }

    @Override
    public String toString() {
        return name;
    }

    // groupingBy 枚举类创建
    public enum CaloriesLevel {
        FAT, NORMAL, DIET
    }


    // 实例化接口创建
    public interface fFunction<S, T, U, R, D> {
        // 使用前面四个参数实现D类型对象的实例化
        D apply(S s, T t, U u, R r);
    }


}


